package com.demo.index;

import java.util.Enumeration;
import java.util.Properties;



import org.apache.log4j.Logger;

import com.demo.util.PropertiesHelper;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;


public class WxInterceptor implements Interceptor {

	public void intercept(ActionInvocation ai) {
        Properties r = PropertiesHelper.loadProperty("/weixin.properties");
        String appId="";
        if(!r.isEmpty()){
            try{
                 appId=r.getProperty("AppId");

            }catch(Exception e){
                Logger.getLogger(WxInterceptor.class).error("无法读取参数/weixin.properties",e);
            }
        }else{
            Logger.getLogger(WxInterceptor.class).error("无法读取 weixin.properties");
        }
		Controller c = ai.getController();
		Enumeration<String> paramNames = c.getRequest().getParameterNames();
		String para="";
        while (paramNames.hasMoreElements()) {
            //错误键，我们规定，所有的错误为 请求字段加上 Msg 
            String key = paramNames.nextElement().toString();
            String value=c.getPara(key);
            para+=key+"="+value+"&";
            System.out.println("===========para============"+ai.getActionKey() );

        }
        if(para.length()>0){
        	para=para.substring(0, para.length()-1);
        }
        System.out.println("===========ai.getActionKey()============="+ai.getActionKey() );
        ai.getController().setAttr("wxurls", ai.getActionKey()+"?" +para);
		ai.invoke();

	}

}
